//
//  JX_WebView.m
//  WebViewTest
//
//  Created by 李珈旭 on 2017/1/19.
//  Copyright © 2017年 jiaxuLI. All rights reserved.
//
#import "JX_WebView.h"

@interface JX_WebView()
@property (nonatomic,strong)NSMutableArray <NSDictionary *>*urls;

@property (nonatomic,assign)WKNavigationActionPolicy state;
@property(nonatomic, copy) WebViewGetUrlBlock block;

@end


static const NSString *ksource = @"source";
static const NSString *ktoUrl = @"toUrl";
static const NSString *kstate = @"state";
static const NSString *kdoBlock = @"doBlock";


@implementation JX_WebView

- (instancetype)initWithFrame:(CGRect)frame configuration:(nonnull WKWebViewConfiguration *)configuration
{
    self = [super initWithFrame:frame configuration:configuration];
    if (self) {
        self.navigationDelegate = self;
        self.urls = [NSMutableArray array];
    }
    return self;
}
//替换URL
-(void)replece:(NSString *)url To:(NSString *)toUrl Do:(void(^)())does{
    
    NSDictionary *urlDic = @{ksource:url,ktoUrl:toUrl,kstate:@"0",kdoBlock:does};
    [self.urls addObject:urlDic];
    
   
}
//检测URL
-(void)check:(NSString *)url Do:(void(^)())does{
    
    NSDictionary *urlDic = @{ksource:url,ktoUrl:@"",kstate:@"1",kdoBlock:does};
    [self.urls addObject:urlDic];
   
}
//拦截URL
-(void)intercept:(NSString *)url Do:(void(^)())does{
    
    NSDictionary *urlDic = @{ksource:url,ktoUrl:@"",kstate:@"0",kdoBlock:does};
    [self.urls addObject:urlDic];
  
}
//包含字符
-(void)contains:(NSString *)str Do:(void(^)())does{
    NSDictionary *urlDic = @{ksource:str,ktoUrl:@"",kstate:@"0",kdoBlock:does};
    [self.urls addObject:urlDic];
}
//Webview加载的开始 完成 失败 代理
-(void)webViewLoadStart:(void (^)(WKWebView *webview,WKNavigation *navigation))start Finish:(void (^)(WKWebView *webview,WKNavigation *navigation))finish Fail:(void (^)(WKWebView *webview,WKNavigation *navigation))fail{
    if (start) {
        _start = start;
    }
    if (finish) {
        _finish = finish;
    }
    if (fail) {
        _fail = fail;
    }
}

#pragma mark *************WKNavigationDelegation**************

-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    NSString *url = navigationAction.request.URL.absoluteString;
    if (self.block) {
        self.block(url);
    }
    for (NSDictionary *dic in self.urls) {
        NSString *sourceUrl = dic[ksource];
        if ([sourceUrl isEqualToString:url]) {
            
            if ( [dic[kstate] isEqualToString:@"0"]) {
                decisionHandler(WKNavigationActionPolicyCancel);
            }else{
                decisionHandler(WKNavigationActionPolicyAllow);
            }
            NSString *to = dic[ktoUrl];
            if(to.length != 0){
                NSURL *newURL = [NSURL URLWithString:dic[ktoUrl]];
                [webView loadRequest:[NSURLRequest requestWithURL:newURL]];
                
            }
            self.does = dic[kdoBlock];
            self.does();
            return;
        }
        if([url containsString:sourceUrl]){
            if ( [dic[kstate] isEqualToString:@"0"]) {
                decisionHandler(WKNavigationActionPolicyCancel);
            }else{
                decisionHandler(WKNavigationActionPolicyAllow);
            }
            NSString *to = dic[ktoUrl];
            if(to.length != 0){
                NSURL *newURL = [NSURL URLWithString:dic[ktoUrl]];
                [webView loadRequest:[NSURLRequest requestWithURL:newURL]];
                
            }
            self.does = dic[kdoBlock];
            self.does();
            return;
        }
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
    _start(webView,navigation);
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    _fail(webView,navigation);
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    _finish(webView,navigation);
}

- (void)getJumpUrl:(WebViewGetUrlBlock)block{
    _block = block;
}

@end
